public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(char val) {
            this.val = val;
        }
    }
    /**
     * 穷举二叉树
     */
    public TreeNode createBinaryTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;

        B.left = D;
        B.right = E;

        E.right = H;

        C.left = F;
        C.right = G;

        return A;
    }
    /**
     * 前序遍历:根 左 右
     */
    public void preOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    /**
     * 中序遍历:左 根 右
     */
    public void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    /**
     * 后序遍历:左 右 根
     */
    public void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }
    /**
     * 求节点个数(遍历)
     */
    public static int nodeSize;
    public void size(TreeNode root) {
        if(root == null) {
            return;
        }

        nodeSize++;
        size(root.left);
        size(root.right);
    }
    /**
     * 求节点个数(子问题思路):左子树个数 + 右子树个数 + 根节点
     */
    public int size2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size2(root.left)
                + size2(root.right) + 1;
    }
    /**
     * 求叶子节点个数(遍历)
     */
    public static int leafSize;
    public void getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }
    /**
     * 求叶子节点个数(子问题):左子树叶子 + 右子树叶子
     */
    public int getLeafNodeCount2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left)
                + getLeafNodeCount2(root.right);
    }
    /**
     * 求第K层节点个数
     */
    public int getKLevelNodeCount(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }

        if(k == 1) {
            return 1;
        }

        return getKLevelNodeCount(root.left,k - 1)
                + getKLevelNodeCount(root.right,k - 1);
    }
    /**
     * 求二叉树高度
     */
    public int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);
        return Math.max(left,right) + 1;
    }
    /**
     * 判断val是否存在
     */
    public TreeNode find(TreeNode root,char val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }

        TreeNode leftVal = find(root.left,val);
        if(leftVal != null) {
            return leftVal;
        }
        TreeNode rightVal = find(root.right,val);
        if(rightVal != null) {
            return rightVal;
        }

        return null;
    }
    /**
     *相同的树:假设p个数为M q个数为N
     * O(min(M,N))
     */
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p != null && q == null || q != null && p == null) {
            return false;
        }

        if(p == null && q == null) {
            return true;
        }

        if(p.val != q.val) {
            return false;
        }

        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }

    /**
     * 是否为子树
     * @param root
     * @param subRoot
     * @return
     * O(M*N)
     */
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null) {
            return false;
        }
        if(isSameTree(root,subRoot)) {
            return true;
        }
        if(isSubtree(root.left,subRoot)) {
            return true;
        }
        if(isSubtree(root.right,subRoot)) {
            return true;
        }
        return false;
    }
    /**
     * 翻转二叉树
     */
    public TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return null;
        }
        if(root.left != null || root.right != null) {
            TreeNode tmp = root.left;
            root.left = root.right;
            root.right = tmp;
        }
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}
